<template>
  <div class="birdGoodsDetails">
    <div class="points_main">
      <div class="goods">
        <div class="Img">
          <img :src="goodsList.pictrue" />
        </div>
        <span class="name">{{ goodsList.goodsName }}</span>
        <div class="center">
          <div class="jifen" v-if="this.$route.query.orderId">
            所需积分:
            <img src="../../assets/image/moliy.png" alt />
            <i>{{ goodsList.point }}</i>
          </div>
          <div class="moliy" v-show="showSku">
            <p>规格:</p>
            <div
              v-for="(item, idx) in goodsList.skuSpecList"
              :key="idx"
              :class="{
                disabled: item.stock == 0,
                normal: item.normal != idx && item.stock != 0,
                checked: item.normal == idx && item.stock != 0,
              }"
              @click="selectSku(item, idx)"
            >
              <span class="bgColor"></span>
              <span>{{ item.skuName }}</span>
            </div>
          </div>
          <div class="ku">库存:{{ goodsList.stock }}</div>
        </div>
      </div>
      <div class="goo">
        <span>采购数量</span>
        <i @click="sub()">-</i>
        <input type="text" v-model="num" @input="showNum()" />
        <i @click="add()">+</i>
      </div>
      <div class="shop_xq">
        <span class="lipin">礼品详情</span>
        <div class="Img_item">
          <img :src="goodsList.pictrue" alt />
          <!-- <img :src="goodsList.minPicture" alt /> -->
        </div>
      </div>
    </div>
    <div class="footer">
      <!-- <div class="foot" @click="goCar()">
        <img src="../../assets/image/shopcar-normal.png" alt />
        <i>礼品车</i>
      </div> -->
      <div class="dui" @click="toWriteOrder()">立即兑换</div>
    </div>
  </div>
</template>

<script>
//引入保存所有接口的url文件
import url from "../../axios/apiUrl";
//引入api.js定义的方法index
import { postRequest } from "../../api/index";
import { getRequest } from "../../api/indexGet";
export default {
  data() {
    return {
      ulIndex: 0, //向上移动几个li
      marginTop: 0, //向上向下移动的距离
      isShow: false, //控制遮罩层marks和大图片是否显示"
      left: 0, //marks左移位置
      top: 0, //marks下移位置
      leftLgImg: 0, //大图lgImg移动的位置
      topLgImg: 0, //大图lgImg移动的位置
      bigImg: "",
      showImg: false, //是否显示小图片的上下按钮
      goodsList: {}, // 商品详情列表
      num: 1,
      sku: "",
      showSku: false,
      orderId:''
    };
  },
  created() {
    getRequest(url.birdGoodsDetails + "/" + this.$route.query.id).then(
      (res) => {
        this.goodsList = res.data.content;
        if (this.goodsList.skuSpecList.length != 0) {
          this.showSku = true;
          this.sku = this.goodsList.skuSpecList[0];
          this.goodsList.skuSpecList.map((obj) => {
            this.$set(obj, "normal", 0);
          });
        }
      }
    );
  },
  methods: {
    // 加
    add() {
      if (this.num < this.goodsList.stock) {
        this.num = parseInt(this.num) + 1;
      } else {
        this.num = this.goodsList.stock;
        this.stock--;
        this.$toast("数量不能再加了！");
      }
    },
    // 减
    sub() {
      if (this.num > 1) {
        this.num = parseInt(this.num) - 1;
      } else {
        this.num = 1;
      }
    },
    // 失焦
    showNum() {
      if (this.num >= this.goodsList.stock) {
        this.num = parseInt(this.goodsList.stock);
      } else if (this.num < 1) {
        this.num = parseInt(1);
      }
    },

    // 提交订单立即兑换
    toWriteOrder() {
      getRequest(url.userPoint).then((res) => {
        if (res.data.content >= this.num * this.goodsList.point) {
          var result = {
            goodsId: this.goodsList.goodsId,
            quantity: this.num,
            skuName: this.sku.skuName,
            skuId: this.sku.skuId,
          };
          sessionStorage.setItem("ponitOrder", JSON.stringify(result));  
            this.$router.push("/birdWriteOrder");
        } else {
          this.$toast("普通积分不足");
        }
      });
    },
    // 去礼品车
    goCar() {
      this.$router.push("/pointsCar");
    },

    selectSku(item, idx) {
      this.goodsList.skuSpecList.map((obj) => {
        obj.normal = idx;
      });
      this.sku = item;
    },
  },
};
</script>
<style lang="less" scoped>
.birdGoodsDetails {
  margin: 0;
  .points_main {
    .goods {
      background: #ffffff;
      padding: 0.26rem;
      border-bottom: 0.26rem solid #f1f2f6;
      .Img {
        text-align: center;
        width: 4.6rem;
        height: 4.6rem;
        margin: 0.2rem auto;
        img {
          width: 4.6rem;
          height: 4.6rem;
        }
      }
      .name {
        width: 100%;
        text-align: center;
        font-size: 0.36rem;
        font-family: PingFang SC;
        font-weight: bold;
        color: #343434;
        line-height: 0.48rem;
        margin: 0 auto;
        margin-top: 0.36rem;
        margin-bottom: 0.36rem;
      }
      .center {
        margin-top: 0.26rem;
        line-height: 0.58rem;

        .jifen {
          color: #343434;
          font-size: 0.36rem;
          font-weight: bold;
          img {
            width: 0.29rem;
            height: 0.29rem;
          }
          i {
            color: #1fb808;
          }
        }
        .moliy {
          width: 7.2rem;
          display: flex;
          align-items: center;
          .skuNames {
            width: 1.68rem;
            height: 0.8rem;
            span {
              width: 1.68rem;
              height: 0.68rem;
              border-radius: 0.35rem;
              text-align: center;
              align-items: center;
              margin-left: 0.2rem;
            }
          }
          .checked {
            width: 1.68rem;
            height: 0.6rem;
            border-radius: 0.35rem;
            text-align: center;
            align-items: center;
            margin-left: 0.2rem;
            border: 1px solid #3ebc2c;
            color: #3ebc2c;
            position: relative;
            .bgImg {
              position: absolute;
              width: 24px;
              height: 22px;
              background: url("../../assets/img/jfDetails1.png") no-repeat 0 0;
              right: -1px;
              bottom: 0px;
            }
          }
          .normal {
            color: #343434;
            border: 1px solid #dadada;
            width: 1.68rem;
            height: 0.6rem;
            line-height: 0.6rem;
            border-radius: 0.35rem;
            text-align: center;
            align-items: center;
            margin-left: 0.2rem;
          }
          .disabled {
            border: 1px solid #efefef;
            color: #9a9a9a;
          }
        }
        .noliy {
          color: #666666;
          font-size: 0.26rem;
          font-weight: 400;
        }
        .ku {
          color: #666666;
          font-size: 0.26rem;
          font-weight: 400;
        }
      }
    }
    .goo {
      height: 1rem;
      line-height: 1rem;
      padding: 0.26rem;
      border-bottom: 0.26rem solid #f1f2f6;
      color: #343434;
      i {
        font-size: 0.42rem;
        padding-left: 0.26rem;
        padding-right: 0.26rem;
      }
      input {
        width: 1.8rem;
        height: 0.7rem;
        border: 1px solid #eaeaea;
        border-radius: 0.35rem;
        text-align: center;
      }
    }
    .shop_xq {
      .lipin {
        height: 1rem;
        line-height: 1rem;
        color: #1eb808;
        border-left: 0.1rem solid #1eb808;
        padding-left: 0.2rem;
      }
      .Img_item {
        text-align: center;
        margin: 0 auto;
         display: flex;
        flex-wrap: wrap;
        align-items: center;
         padding-bottom: 1rem;
        img {
          width: 100%;
          height: 100%;
          box-sizing: border-box;
          margin: 0.1rem auto;
        }
      }
    }
  }
  .footer {
    z-index: 9999;
    height: 1rem;
    display: flex;
    justify-content: flex-end;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    border: 0.01rem solid #f1f1f1;
    background: #ffffff;
    text-align: -webkit-center;
    .foot {
      width: 2.8rem;
      height: 1rem;
      margin: 0 auto;
      // display: flex;
      // flex-direction: column;
      img {
        width: 0.48rem;
        height: 0.48rem;
        margin-bottom: 0.12rem;
      }
    }
    .jia {
      width: 2.8rem;
      height: 1rem;
      background: #1eb808;
      line-height: 1rem;
      align-items: center;
      text-align: center;
      color: #ffffff;
    }
    .dui {
      width: 2.8rem;
      height: 1rem;
      float: left;
      background: #fe323c;
      align-items: center;
      text-align: center;
      color: #ffffff;
      line-height: 1rem;
    }
  }
}
</style>